home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / WFC010.ZIP / SRC / CNETSHAR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-07  |  6.9 KB  |  299 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like as long as you don't try to sell it.
  10. **
  11. ** Any attempt to sell WFC in source code form must have the permission
  12. ** of the original author. You can produce commercial executables with
  13. ** WFC but you can't sell WFC.
  14. **
  15. ** Copyright, 1995, Samuel R. Blackburn
  16. **
  17. ** $Workfile: $
  18. ** $Revision: $
  19. ** $Modtime: $
  20. */
  21.  
  22. #if defined( _DEBUG )
  23. #undef THIS_FILE
  24. static char BASED_CODE THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27. /*
  28. ** CNetworkConnectionInformation stuff
  29. */
  30.  
  31. IMPLEMENT_SERIAL( CNetworkShareInformation, CObject, 1 )
  32. IMPLEMENT_SERIAL( CNetworkShares, CNetwork, 1 )
  33.  
  34. #if defined( _DEBUG )
  35. #define new DEBUG_NEW
  36. #endif
  37.  
  38. CNetworkShareInformation::CNetworkShareInformation()
  39. {
  40.    m_Initialize();
  41. }
  42.  
  43. /*
  44. ** Can't make Copy take a const pointer because Microsoft screwed up the 
  45. ** net API header files...
  46. */
  47.  
  48. CNetworkShareInformation::CNetworkShareInformation( SHARE_INFO_2 *source )
  49. {
  50.    Copy( source );
  51. }
  52.  
  53. CNetworkShareInformation::CNetworkShareInformation( const CNetworkShareInformation& source )
  54. {
  55.    Copy( source );
  56. }
  57.  
  58. CNetworkShareInformation::~CNetworkShareInformation()
  59. {
  60.    m_Initialize();
  61. }
  62.  
  63. /*
  64. ** Can't make Copy take a const pointer because Microsoft screwed up the 
  65. ** net API header files...
  66. */
  67.  
  68. void CNetworkShareInformation::Copy( SHARE_INFO_2 *source )
  69. {
  70.    ASSERT( source != NULL );
  71.  
  72.    if ( source == NULL )
  73.    {
  74.       m_Initialize();
  75.       return;
  76.    }
  77.  
  78. #if ! defined( UNICODE )
  79.    ::UNICODE_to_ASCII( (LPCWSTR) source->shi2_netname, source->shi2_netname );
  80.    ::UNICODE_to_ASCII( (LPCWSTR) source->shi2_remark,  source->shi2_remark  );
  81.    ::UNICODE_to_ASCII( (LPCWSTR) source->shi2_path,    source->shi2_path    );
  82.    ::UNICODE_to_ASCII( (LPCWSTR) source->shi2_passwd,  source->shi2_passwd  );
  83. #endif
  84.  
  85.    NetworkName         = source->shi2_netname;
  86.    Type                = source->shi2_type;
  87.    Remark              = source->shi2_remark;
  88.    Permissions         = source->shi2_permissions;
  89.    MaximumNumberOfUses = source->shi2_max_uses;
  90.    CurrentNumberOfUses = source->shi2_current_uses;
  91.    PathName            = source->shi2_path;
  92.    Password            = source->shi2_passwd;
  93.  
  94. #if ! defined( UNICODE )
  95.    ::ASCII_to_UNICODE( source->shi2_netname, (LPWSTR) source->shi2_netname );
  96.    ::ASCII_to_UNICODE( source->shi2_remark,  (LPWSTR) source->shi2_remark  );
  97.    ::ASCII_to_UNICODE( source->shi2_path,    (LPWSTR) source->shi2_path    );
  98.    ::ASCII_to_UNICODE( source->shi2_passwd,  (LPWSTR) source->shi2_passwd  );
  99. #endif
  100. }
  101.  
  102. void CNetworkShareInformation::Copy( const CNetworkShareInformation& source )
  103. {
  104.    ASSERT( this != &source );
  105.  
  106.    /*
  107.    ** Make sure we ain't copying ourselves
  108.    */
  109.  
  110.    if ( this == &source )
  111.    {
  112.       return;
  113.    }
  114.  
  115.    NetworkName         = source.NetworkName;
  116.    Type                = source.Type;
  117.    Remark              = source.Remark;
  118.    Permissions         = source.Permissions;
  119.    MaximumNumberOfUses = source.MaximumNumberOfUses;
  120.    CurrentNumberOfUses = source.CurrentNumberOfUses;
  121.    PathName            = source.PathName;
  122.    Password            = source.Password;
  123. }
  124.  
  125. void CNetworkShareInformation::Empty( void )
  126. {
  127.    m_Initialize();
  128. }
  129.  
  130. void CNetworkShareInformation::m_Initialize( void )
  131. {
  132.    NetworkName.Empty();
  133.    Type                = 0;
  134.    Remark.Empty();
  135.    Permissions         = 0;
  136.    MaximumNumberOfUses = 0;
  137.    CurrentNumberOfUses = 0;
  138.    PathName.Empty();
  139.    Password.Empty();
  140. }
  141.  
  142. void CNetworkShareInformation::Serialize( CArchive& archive )
  143. {
  144.    CObject::Serialize( archive );
  145.  
  146.    if ( archive.IsStoring() )
  147.    {
  148.       archive << NetworkName;
  149.       archive << Type;
  150.       archive << Remark;
  151.       archive << Permissions;
  152.       archive << MaximumNumberOfUses;
  153.       archive << CurrentNumberOfUses;
  154.       archive << PathName;
  155.       archive << Password;
  156.    }
  157.    else
  158.    {
  159.       archive >> NetworkName;
  160.       archive >> Type;
  161.       archive >> Remark;
  162.       archive >> Permissions;
  163.       archive >> MaximumNumberOfUses;
  164.       archive >> CurrentNumberOfUses;
  165.       archive >> PathName;
  166.       archive >> Password;
  167.    }
  168. }
  169.  
  170. /*
  171. ** CNetworkConnections Stuff
  172. */
  173.  
  174. CNetworkShares::CNetworkShares()
  175. {
  176.    m_Initialize();
  177. }
  178.  
  179. CNetworkShares::CNetworkShares( LPCTSTR machine_name )
  180. {
  181.    m_Initialize();
  182.    Open( machine_name );
  183. }
  184.  
  185. CNetworkShares::~CNetworkShares()
  186. {
  187.    Close();
  188.    m_Initialize();
  189. }
  190.  
  191. BOOL CNetworkShares::Add( CNetworkShareInformation& share_to_add )
  192. {
  193.    // NetShareAdd
  194.  
  195.    //m_ErrorCode = ::NetShareAdd( (LPTSTR) m_WideMachineName,
  196.  
  197.    if ( m_ErrorCode == NERR_Success )
  198.    {
  199.       return( TRUE );
  200.    }
  201.    else
  202.    {
  203.       return( FALSE );
  204.    }
  205. }
  206.  
  207. void CNetworkShares::Close( void )
  208. {
  209.    CNetwork::Close();
  210.  
  211.    if ( m_2InformationBuffer != NULL )
  212.    {
  213.       ::NetApiBufferFree( m_2InformationBuffer );
  214.       m_2InformationBuffer = NULL;
  215.    }
  216. }
  217.  
  218. BOOL CNetworkShares::Delete( CNetworkShareInformation& share_to_delete )
  219. {
  220.    // NetShareDel
  221.  
  222.    //m_ErrorCode = ::NetShareDel( (LPTSTR) m_WideMachineName,
  223.  
  224.    if ( m_ErrorCode == NERR_Success )
  225.    {
  226.       return( TRUE );
  227.    }
  228.    else
  229.    {
  230.       return( FALSE );
  231.    }
  232. }
  233.  
  234. void CNetworkShares::m_Initialize( void )
  235. {
  236.    m_ErrorCode               = 0;
  237.    m_2InformationBuffer    = NULL;
  238.    m_2ResumeHandle         = 0;
  239.    m_2CurrentEntryNumber   = 0;
  240.    m_2NumberOfEntriesRead  = 0;
  241.    m_2TotalNumberOfEntries = 0;
  242. }
  243.  
  244. BOOL CNetworkShares::Enumerate( void )
  245. {
  246.    if ( m_2InformationBuffer != NULL )
  247.    {
  248.       ::NetApiBufferFree( m_2InformationBuffer );
  249.       m_2InformationBuffer = NULL;
  250.    }
  251.  
  252.    m_2CurrentEntryNumber   = 0;
  253.    m_2NumberOfEntriesRead  = 0;
  254.    m_2ResumeHandle         = 0;
  255.    m_2TotalNumberOfEntries = 0;
  256.  
  257.    m_ErrorCode = ::NetShareEnum( (LPTSTR) m_WideMachineName,
  258.                                           2, 
  259.                               (LPBYTE *) &m_2InformationBuffer,
  260.                                           65535,
  261.                                          &m_2NumberOfEntriesRead,
  262.                                          &m_2TotalNumberOfEntries,
  263.                                          &m_2ResumeHandle );
  264.  
  265.    if ( m_ErrorCode != NERR_Success || m_2InformationBuffer == NULL )
  266.    {
  267.       return( FALSE );
  268.    }
  269.  
  270.    return( TRUE );
  271. }
  272.  
  273. BOOL CNetworkShares::GetNext( CNetworkShareInformation& information )
  274. {
  275.    if ( m_2CurrentEntryNumber < m_2TotalNumberOfEntries )
  276.    {
  277.       information.Copy( &m_2InformationBuffer[ m_2CurrentEntryNumber ] );
  278.       m_2CurrentEntryNumber++;
  279.       return( TRUE );
  280.    }
  281.  
  282.    information.Empty();
  283.    return( FALSE );
  284. }
  285.  
  286. void CNetworkShares::Serialize( CArchive& archive )
  287. {
  288.    CNetwork::Serialize( archive );
  289.  
  290.    if ( archive.IsStoring() )
  291.    {
  292.       archive << m_ErrorCode;
  293.    }
  294.    else
  295.    {
  296.       archive >> m_ErrorCode;
  297.    }
  298. }
  299.